package com.anbinh.doctor.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentLogin extends Fragment {
	private View mView;
	private static FragmentLogin fragmentLogin = null;
	private CircleProgressBar mLoadding;

	public static FragmentLogin newInstance() {
		fragmentLogin = new FragmentLogin();
		return fragmentLogin;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mLoadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		Button btnLogin = (Button) mView.findViewById(R.id.btnDangnhap);
		final EditText edPhone = (EditText) mView.findViewById(R.id.edPhone);
		final EditText edPass = (EditText) mView.findViewById(R.id.edPass);

		Utils.setOnTouchHideKeyboard(getActivity(), mView);

		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				final String mPhone = edPhone.getText().toString().trim();
				if (mPhone.isEmpty()) {
					Utils.showMessage(
							getActivity(),
							getResources().getString(
									R.string.vui_long_nhap_so_dien_thoai));
					return;
				}
				final String mPass = edPass.getText().toString().trim();
				if (mPass.isEmpty()) {
					Utils.showMessage(
							getActivity(),
							getResources().getString(
									R.string.vui_long_nhap_mat_khau));
					return;
				}
				onLoging(mPhone, mPass);

			}
		});

		final LinearLayout LoginBox = (LinearLayout) mView
				.findViewById(R.id.loginForm);
		LoginBox.setVisibility(View.INVISIBLE);

		Animation animTranslate = AnimationUtils.loadAnimation(getActivity(),
				R.anim.translate);
		animTranslate.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation arg0) {
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				LoginBox.setVisibility(View.VISIBLE);
				Animation animFade = AnimationUtils.loadAnimation(
						getActivity(), R.anim.fade);
				LoginBox.startAnimation(animFade);
				UserLogin data = Utils.getAccountModel(getActivity(),
						Utils.KEY_USER);
				if (data != null) {
					gotoMainForm();
				}
			}
		});
		ImageView imgLogo = (ImageView) mView.findViewById(R.id.logo);
		imgLogo.startAnimation(animTranslate);
		View btnRegister = (View) mView.findViewById(R.id.btnRegister);
		btnRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				gotoCreateAcc(Utils.CREATE_ACC, "");
			}
		});

	}

	protected void onLoging(final String mPhone, final String mPass) {
		Utils.disableEnableControls(false, mView);
		mLoadding.setVisibility(View.VISIBLE);
		ServerController.getInstance(getActivity()).reuquestDoctorLogin(mPhone,
				mPass, new OnRequest<UserLogin>() {

					@Override
					public void onSuccess(UserLogin data) {
						Utils.disableEnableControls(true, mView);
						mLoadding.setVisibility(View.INVISIBLE);
						if (data.getResponsecode() >= 0) {
							UserLogin user = data;
							user.setUsername(mPhone);
							user.setPassword(mPass);
							Utils.updateData(getActivity(), Utils.KEY_USER,
									user);

							gotoMainForm();

						} else if (data.getResponsecode() == -2) {
							gotoActiveForm(Utils.ACTIVE_ACC, mPhone);
							Utils.showMessage(getActivity(),
									data.getDescription());
						} else {
							Utils.showMessage(getActivity(),
									data.getDescription());
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						Utils.disableEnableControls(true, mView);
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.showMessage(
								getActivity(),
								getResources().getString(
										R.string.vui_long_kiem_tra_lai_ket_noi));
					}
				});
	}

	public void gotoCreateAcc(int code, String phone) {
		Bundle data = new Bundle();
		data.putInt(Utils.KEY, code);
		data.putString(Utils.PHONE, phone);

		getActivity()
				.getSupportFragmentManager()
				.beginTransaction()
				.replace(R.id.fragment_container,
						FragmentRegister.newInstance(data), "TAG_REGISTER")
				.addToBackStack(null).commit();
	}

	public void gotoActiveForm(int code, String phone) {
		Bundle data = new Bundle();
		data.putInt(Utils.KEY, code);
		data.putString(Utils.PHONE, phone);

		getActivity()
				.getSupportFragmentManager()
				.beginTransaction()
				.replace(R.id.fragment_container,
						FragmentActiveAccount.newInstance(data), "TAG_REGISTER")
				.commit();
	}

	public void gotoMainForm() {
		/*
		 * Intent intent = new Intent(getActivity(), MainActivity.class);
		 * startActivity(intent); getActivity().finish();
		 */
		getActivity()
				.getSupportFragmentManager()
				.beginTransaction()
				.replace(R.id.fragment_container, FragmentMain.newInstance(),
						"TAG_MAIN").commit();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_login, null);
		return mView;
	}

}
